package com.v.LogOutPutFormat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class LogRecoderWrite extends RecordWriter<Text, NullWritable> {


    private  FSDataOutputStream atguigu;
    private  FSDataOutputStream other;

    public LogRecoderWrite(TaskAttemptContext job) {



        //创建两条流
        //FileSystem是HDFS客户端
        try {
            //创建配置信息
            FileSystem fileSystem = FileSystem.get(job.getConfiguration());

            atguigu = fileSystem.create(new Path("E:\\hadoop_Test\\OutputFile\\io_distinction\\atguigu.log"));

            other = fileSystem.create(new Path("E:\\hadoop_Test\\OutputFile\\io_distinction\\other.log"));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {

        String log = key.toString();
        if(log.contains("atguigu")){
            atguigu.writeBytes(log + "\n");
        } else {
            other.writeBytes(log+ "\n");
        }

    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(atguigu);
        IOUtils.closeStream(other);
    }
}
